package com.ruoyi.web.zbAdmin.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.mongodb.client.result.UpdateResult;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.web.zbAdmin.controller.param.UserViewCountQuery;
import com.ruoyi.web.zbAdmin.domain.CaiBaseUserKeyword;
import com.ruoyi.web.zbAdmin.mapper.CaiBaseUserKeywordMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liandyao
 * @date 2022/12/15 11:12
 */
@Api(tags="才能招标-用户订阅管理")
@RestController
@RequestMapping("/zbApi/userKeyword")
@Slf4j
public class CaiBaseUserKeywordController {

    @Autowired
    private CaiBaseUserKeywordMapper userMapper;

    public static final String collectionName = "cai_base_user_keyword";

    @Autowired
    private RedisCache redisCache;

    /**
     * 获取用户列表
     */
    @ApiOperation("获取用户订阅列表")
    @GetMapping("/list")
    public TableDataInfo list(UserViewCountQuery userQuery) {

        log.info("=====>"+JSONUtil.toJsonStr(userQuery));
        MongoTemplate mongoTemplate = this.userMapper.getMongoTemplate();

        Query query = new Query();
        if(StrUtil.isNotBlank(userQuery.getUserId())){
            query.addCriteria(Criteria.where("userId").is(userQuery.getUserId()));
        }
        // 设置起始页和每页查询条数
        Pageable pageable = PageRequest.of(userQuery.getCurPage(), userQuery.getPageSize(), Sort.by(Sort.Order.desc("updateDate")));
        // 查询记录总数
        long totalCount = mongoTemplate.count(query,collectionName);
        //查询分页后的记录
        List<CaiBaseUserKeyword> result = mongoTemplate.find(query.with(pageable), CaiBaseUserKeyword.class, collectionName);

        return TableDataInfo.success(result,totalCount);
    }




    @ApiOperation("获取用户订阅详细")
    @GetMapping("/{userId}")
    public AjaxResult getById(@PathVariable String userId) {
        MongoTemplate mongoTemplate = this.userMapper.getMongoTemplate();
        CaiBaseUserKeyword user = mongoTemplate.findById(userId,CaiBaseUserKeyword.class,collectionName);
        if(user==null){
            return AjaxResult.error("不存在!");
        }
        return AjaxResult.success("成功",user);
    }



    @ApiOperation("删除用户订阅信息")
    @PostMapping("/delete/{userId}")
    public AjaxResult delete(@PathVariable String userId) {

        MongoTemplate mongoTemplate = this.userMapper.getMongoTemplate();
        /**
         * 删除这条招标相关的所有信息
         */
        UpdateResult result=mongoTemplate.upsert(Query.query(Criteria.where("userId").is(userId)), Update.update("delFlag",CaiBaseUserKeyword.IS_DEL_YES),collectionName);

        return AjaxResult.success("删除成功",result.getMatchedCount());

    }
}
